#include "Pen.h"
#include <PA9.h>       // Include for PA_Lib

// Include the pen sounds
#include "pen_touch_hit.h"  
#include "pen_touch_miss.h"  
#include "pen_touch_illegal.h" 

// Constructor
Pen::Pen()
{
	
	// time left before pen can draw again
	m_iDelay = 0;

};

// Get current delay
unsigned short int Pen::GetDelay()
{
	return m_iDelay;
};
	
// The pen touches the screen at this position
void Pen::Press(int x, int y, TouchType touchType)
{

	// The pen hits creature
	if (touchType == HIT)
	{
		// Play hit sound
		PA_PlaySimpleSound(pen_touch_hit);

		// Move the pen graphics to this position
		PA_SetSpriteXY(LOWER_SCREEN,SPRITE_PEN_HIT, x-PEN_SPRITE_SIZE/2, y-PEN_SPRITE_SIZE/2);
		PA_SetSpriteXY(UPPER_SCREEN,SPRITE_PEN_HIT, x-PEN_SPRITE_SIZE/2, y-PEN_SPRITE_SIZE/2);
	
		// Start run the pen animation (first frame, last frame, speed)
		PA_StartSpriteAnimEx(LOWER_SCREEN, SPRITE_PEN_HIT, 0, 3, 10, ANIM_ONESHOT);
		PA_StartSpriteAnimEx(UPPER_SCREEN, SPRITE_PEN_HIT, 0, 3, 10, ANIM_ONESHOT);
	};

	// The pen hits creature
	if (touchType == MISS)
	{
		// Play hit sound
		PA_PlaySimpleSound(pen_touch_hit);
		
		// Move the pen graphics to this position
		PA_SetSpriteXY(LOWER_SCREEN,SPRITE_PEN_MISS, x-PEN_SPRITE_SIZE/2, y-PEN_SPRITE_SIZE/2);
		PA_SetSpriteXY(UPPER_SCREEN,SPRITE_PEN_MISS, x-PEN_SPRITE_SIZE/2, y-PEN_SPRITE_SIZE/2);

		// Start run the pen animation (first frame, last frame, speed)
		PA_StartSpriteAnimEx(LOWER_SCREEN, SPRITE_PEN_MISS, 0, 3, 10, ANIM_ONESHOT);
		PA_StartSpriteAnimEx(UPPER_SCREEN, SPRITE_PEN_MISS, 0, 3, 10, ANIM_ONESHOT);
	};

	// The pen hits creature
	if (touchType == ILLEGAL)
	{
		// Play hit sound
		PA_PlaySimpleSound(pen_touch_illegal);
		
		// Move the pen graphics to this position
		PA_SetSpriteXY(LOWER_SCREEN,SPRITE_PEN_ILLEGAL, x-PEN_SPRITE_SIZE/2, y-PEN_SPRITE_SIZE/2);
		PA_SetSpriteXY(UPPER_SCREEN,SPRITE_PEN_ILLEGAL, x-PEN_SPRITE_SIZE/2, y-PEN_SPRITE_SIZE/2);

		// Start run the pen animation (first frame, last frame, speed)
		PA_StartSpriteAnimEx(LOWER_SCREEN, SPRITE_PEN_ILLEGAL, 0, 3, 10, ANIM_ONESHOT);
		PA_StartSpriteAnimEx(UPPER_SCREEN, SPRITE_PEN_ILLEGAL, 0, 3, 10, ANIM_ONESHOT);
	};	
	
	// Enforce speed limit (No super fast touching!)
	m_iDelay = PEN_SPEED_LIMIT;
	
};

// Can the pen be used at this time?
bool Pen::IsUsable()
{
	// Pen can be used if delay is 0 (or less)
	if (m_iDelay <= 0)
	{
		return true;
	} else {
		return false;
	};
};
	
// Time passes
void Pen::Tick()
{
	if (m_iDelay > 0)
	{
		// Count down delay time
		m_iDelay--;
	};
};	

